package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        //下单
        orderService.submit(orders);

        return R.success("下单成功");
    }

    @GetMapping("page")
    public R<Page> getpage(int page,int pageSize,String number){
        Page<Orders> page1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(number),Orders::getNumber,number);
//        wrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(page1,wrapper);


        List<Orders> orders = page1.getRecords();

        Page<OrdersDto> page2 = new Page<>(page, pageSize);


        return R.success(page1);

    }

    @GetMapping("/userPage")
    public R<Page> userpage(int page,int pageSize){
        Page<Orders> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();

        wrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(page1,wrapper);

        return R.success(page1);

    }


}
